package com.study.contoller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.R;
import com.study.entity.School;
import com.study.service.SchoolService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.UUID;

/**
<<<<<<< HEAD
 * @author:zt
 * @createDate: 2022/6/20
 * @author lxl
 * @version 2022-06-21 12:14
 * 前台使用的接口加了front
 */
@RestController
@RequestMapping("/api/school")
public class SchoolController {
    private static Logger log = LoggerFactory.getLogger(SchoolController.class);

    @Autowired
    private SchoolService schoolService;




    @GetMapping("page-list")
    public R page(String countiesName, String schoolName
    ) {
//        PageInfo<Object> objectPageInfo = PageHelper.startPage(1, 100).doSelectPageInfo(() -> {
//            schoolService.page(countiesName, schoolName);
//        });
//        return R.ok().setData(objectPageInfo);

        return R.ok().setData(schoolService.page(countiesName, schoolName));
    }


    /**
     * 添加
     */
    @PostMapping("add")
    public R insert(@RequestBody School school) {
        try {

            school.setSchoolId(UUID.randomUUID().getMostSignificantBits() & Long.MAX_VALUE/1000);
            schoolService.insertSelective(school);
            return R.ok();
        } catch (Exception e) {
            return R.error().setMessage("添加失败");
        }
    }

    /**
     * 根据主键删除
     */
    @DeleteMapping("{id}")
    public R del(@PathVariable Long id) {
        try {
            schoolService.deleteByPrimaryKey(id);
            return R.ok();
        } catch (Exception e) {
            return R.error().setMessage("删除失败");
        }
    }

    /**
     * 修改
     */
    @PutMapping
    public R updateKey(@RequestBody School school){
        try{
            schoolService.updateByPrimaryKeySelective(school);
            return R.ok();
        }catch (Exception e){
            return R.error().setMessage("修改失败");
        }
    }

    @GetMapping("select")
    public R select(){
        return R.ok().setData(schoolService.select());
    }



    @PostMapping("front/insert")
    public R frontInsert(@RequestBody School school) {
        return R.ok().setData(schoolService.insert(school));
    }
    
    @DeleteMapping("front/{schoolId}")
    public R frontDelete(@PathVariable Long schoolId) {
        return R.ok().setData(schoolService.deleteByPrimaryKey(schoolId));
    }
    
    @PutMapping("front/update-by-primary")
    public R frontUpdateByPrimary(@RequestBody School school) {
        return R.ok().setData(schoolService.updateByPrimaryKey(school));
    }
    
    
    @GetMapping("front/selects")
    public R frontSelects(School school) {
        return R.ok().setData(schoolService.selects(school));
    }
    
    @GetMapping("front/select-by-primary/{schoolId}")
    public R frontSelectByPrimary(@PathVariable Long schoolId) {
        return R.ok().setData(schoolService.selectByPrimaryKey(schoolId));
    }
}
